<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户订单号" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入商户订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易状态" prop="tradeStatus">
        <el-select v-model="queryParams.tradeStatus" placeholder="请选择交易状态" clearable>
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:payment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paymentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="60" />
      <el-table-column label="商户订单号" align="center" prop="outTradeNo" width="180" />
      <el-table-column label="支付宝交易号" align="center" prop="tradeNo" width="180" />
      <el-table-column label="商品名称" align="center" prop="productName" :show-overflow-tooltip="true" />
      <el-table-column label="订单金额" align="center" prop="totalAmount">
        <template slot-scope="scope">
          ¥{{ scope.row.totalAmount }}
        </template>
      </el-table-column>
      <el-table-column label="交易状态" align="center" prop="tradeStatus">
        <template slot-scope="scope">
          <el-tag :type="statusTagType(scope.row.tradeStatus)">{{ statusFormat(scope.row.tradeStatus) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="gmtPayment" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtPayment) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:payment:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleViewOrder(scope.row)"
            v-hasPermi="['system:order:query']"
          >查看订单</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看支付详情对话框 -->
    <el-dialog title="支付详情" :visible.sync="viewDialogVisible" width="700px" append-to-body>
      <el-descriptions border :column="2" direction="vertical">
        <el-descriptions-item label="支付ID" :span="2">{{ viewForm.id }}</el-descriptions-item>
        <el-descriptions-item label="商户订单号">{{ viewForm.outTradeNo }}</el-descriptions-item>
        <el-descriptions-item label="支付宝交易号">{{ viewForm.tradeNo }}</el-descriptions-item>
        <el-descriptions-item label="商品名称" :span="2">{{ viewForm.productName }}</el-descriptions-item>
        <el-descriptions-item label="订单金额">¥{{ viewForm.totalAmount }}</el-descriptions-item>
        <el-descriptions-item label="交易状态">
          <el-tag :type="statusTagType(viewForm.tradeStatus)">{{ statusFormat(viewForm.tradeStatus) }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="买家ID">{{ viewForm.buyerId }}</el-descriptions-item>
        <el-descriptions-item label="卖家ID">{{ viewForm.sellerId }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(viewForm.gmtCreate) }}</el-descriptions-item>
        <el-descriptions-item label="支付时间">{{ parseTime(viewForm.gmtPayment) }}</el-descriptions-item>
        <el-descriptions-item label="最后修改时间" :span="2">{{ parseTime(viewForm.gmtModified) }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewDialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayments, getPayment } from "@/api/company/payment";

export default {
  name: "PaymentSettlement",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付结算表格数据
      paymentList: [],
      // 是否显示查看弹出层
      viewDialogVisible: false,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [
        { value: "WAIT_BUYER_PAY", label: "交易创建" },
        { value: "TRADE_CLOSED", label: "交易关闭" },
        { value: "TRADE_SUCCESS", label: "支付成功" },
        { value: "TRADE_FINISHED", label: "交易完成" }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outTradeNo: null,
        tradeStatus: null
      },
      // 查看表单
      viewForm: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询支付结算列表 */
    getList() {
      this.loading = true;
      listPayments(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    // 交易状态字典翻译
    statusFormat(status) {
      const statusMap = {
        'WAIT_BUYER_PAY': '交易创建',
        'TRADE_CLOSED': '交易关闭',
        'TRADE_SUCCESS': '支付成功',
        'TRADE_FINISHED': '交易完成'
      };
      return statusMap[status] || status;
    },
    // 交易状态标签类型
    statusTagType(status) {
      const statusMap = {
        'WAIT_BUYER_PAY': 'info',
        'TRADE_CLOSED': 'danger',
        'TRADE_SUCCESS': 'success',
        'TRADE_FINISHED': 'success'
      };
      return statusMap[status] || '';
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 查看详情操作 */
    handleView(row) {
      getPayment(row.id).then(response => {
        this.viewForm = response.data;
        this.viewDialogVisible = true;
      });
    },
    /** 查看关联订单 */
    handleViewOrder(row) {
      // 跳转到订单详情页，根据商户订单号查询
      this.$router.push({
        path: '/company/orderManage',
        query: {
          orderNumber: row.outTradeNo
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/payment/export', {
        ...this.queryParams
      }, `payment_settlement_${new Date().getTime()}.xlsx`);
    }
  }
};
</script> 